// This script generates the meta.js file
// that provides any package & env information needed at runtime

// meta.js is placed in src/meta.js
import fs from "node:fs/promises"
import path from "node:path"
import { fileURLToPath } from "node:url"

const __file = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__file)

const packageJson = await fs.readFile(path.resolve(__dirname, "../package.json"), "utf-8")
const packageJsonData = JSON.parse(packageJson)
const { version } = packageJsonData
if (!version) throw new Error("package.json must have a version property")

const marketplaceManifest = await fs.readFile(
	path.resolve(__dirname, "../marketplace-manifest.json"),
	"utf-8"
)
const marketplaceManifestData = JSON.parse(marketplaceManifest)
const { id: marketplaceID } = marketplaceManifestData

const destination = path.resolve(__dirname, "../src/meta.js")

const metaJs = `// file generated by build script - do not commit
export const PARAGLIDE_SVELTEKIT_VERSION = ${JSON.stringify(version)}
export const PARAGLIDE_SVELTEKIT_MARKETPLACE_ID = ${JSON.stringify(marketplaceID)}
`

await fs.writeFile(destination, metaJs)
